﻿#ifndef AUTHORIZATIONGUARD_H
#define AUTHORIZATIONGUARD_H

#include <QMutex>
#include <QObject>

class AuthorizationGuard : public QObject
{
    Q_OBJECT
public:
    // 获取单例实例
    static AuthorizationGuard& getInstance();
    // 删除单例
    static void deleteInstance();

    void setSecretKey(QString &key);

    bool isAuthorised();

    /**
     * @brief getLatestHintText 获取最新提示信息
     * @return
     */
    QString getLatestHintText() const;

    /**
     * @brief setAuthorizationKeyFilePath 设置Key文件路径
     * @param value
     */
    void setAuthorizationKeyFilePath(const QString &value);
    /**
     * @brief setAuthorizationLicenseFilePath 设置证书文件路径
     * @param value
     */
    void setAuthorizationLicenseFilePath(const QString &value);
    /**
     * @brief setAuthorizationKey 设置Key数据
     * @param value
     */
    void setAuthorizationKey(const QString &value);
    /**
     * @brief setAuthorizationLicense 设置证书数据
     * @param value
     */
    void setAuthorizationLicense(const QString &value);
    /**
     * @brief verifyAuthorizationLicense 校验证书
     * @return
     */
    bool verifyAuthorizationLicense();
    /**
     * @brief generateAuthorizationLicense 生成证书
     * @return
     */
    bool generateAuthorizationLicense();
private:
    explicit AuthorizationGuard(QObject *parent = nullptr);
    ~AuthorizationGuard();

    static AuthorizationGuard* instance;
    static QMutex mutex;
private:
    bool verify(); //校验
    bool generate(); //生成

    void writeAuthorizationKeyFile();
    bool writeAuthorizationLicenseFile();

    bool readAuthorizationKeyFile();
    bool readAuthorizationLicenseFile();
private:
    QString authorizationKeyFilePath; //key文件路径
    QString authorizationLicenseFilePath; //许可文件路径

    QString authorizationKey;
    QString authorizationLicense;

    QString secretKey;
    bool isFirstVerify;
    bool authorised;
    QString latestHintText;
};

#endif // AUTHORIZATIONGUARD_H
